package com.mkl.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mkl.business.domain.Goods;
import com.mkl.business.domain.Inport;
import com.mkl.business.domain.Provider;
import com.mkl.business.mapper.InportMapper;
import com.mkl.business.service.GoodsService;
import com.mkl.business.service.InportService;
import com.mkl.business.service.ProviderService;
import com.mkl.business.vo.InportVo;
import com.mkl.system.common.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * @author king
 * @version 1.0
 * @date 2020/4/8 11:57
 */
@Service
public class InportServiceImpl extends ServiceImpl<InportMapper, Inport> implements InportService {

    @Autowired
    private InportMapper inportMapper;

    @Autowired
    private ProviderService providerService;

    @Autowired
    private GoodsService goodsService;

    @Override
    public DataGridView queryAllInport(InportVo inportVo) {
        IPage<Inport> page = new Page<>(inportVo.getPage(), inportVo.getLimit());
        QueryWrapper<Inport> qw = new QueryWrapper<>();
        qw.eq(inportVo.getGoodsid() != null, "goodsid", inportVo.getGoodsid());
        qw.eq(inportVo.getProviderid() != null, "providerid", inportVo.getProviderid());
        qw.ge(inportVo.getStartTime() != null, "inporttime", inportVo.getStartTime());
        qw.le(inportVo.getEndTime() != null, "inporttime", inportVo.getEndTime());
        qw.orderByDesc("inporttime");
        inportMapper.selectPage(page, qw);
        List<Inport> records = page.getRecords();
        for (Inport record : records) {
            if (null != record.getGoodsid()) {
                Goods goods = goodsService.getById(record.getGoodsid());
                record.setGoodsname(goods.getGoodsname());
                record.setSize(goods.getSize());
            }
            if (null != record.getProviderid()) {
                Provider provider = providerService.getById(record.getProviderid());
                record.setProvidername(provider.getProvidername());
            }
        }
        return new DataGridView(page.getTotal(), records);
    }

    @Override
    public Inport saveInport(Inport inport) {
        inportMapper.insert(inport);
        //更新库存
        Integer goodsId = inport.getGoodsid();
        Goods goods = goodsService.getById(goodsId);
        goods.setNumber(goods.getNumber() + inport.getNumber());
        goodsService.updateGoods(goods);
        return inport;
    }

    @Override
    public Inport updateInport(Inport inport) {
        Inport oldObj = inportMapper.selectById(inport.getId());
        Goods goods = goodsService.getById(oldObj.getGoodsid());
        goods.setNumber(goods.getNumber() - oldObj.getNumber() + inport.getNumber());
        System.out.println("goods.getNumber()    "+goods.getNumber()+"oldObj.getNumber()    "+oldObj.getNumber()+"inport.getNumber()   "+inport.getNumber());
        goodsService.updateGoods(goods);
        inportMapper.updateById(inport);
        return inport;
    }

    @Override
    public boolean removeById(Serializable id) {
        Inport inport = inportMapper.selectById(id);
        Goods goods = goodsService.getById(inport.getGoodsid());
        goods.setNumber(goods.getNumber() - inport.getNumber());
        goodsService.updateGoods(goods);
        return super.removeById(id);
    }

}
